package net.mysoft.framework.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.mysoft.budgetsoft.system.domain.TUser;

import org.springframework.util.StringUtils;

@SuppressWarnings({"rawtypes", "unchecked"})
public class Lists {
	
	private List list;
	
	public Lists() {
		list = new ArrayList();
	}
	
	public static List newArrayList() {
		return new ArrayList();
	}
	
	public static List newArrayList(Object...objs) {
		List list = new ArrayList();
		int len = objs.length;
		if(objs!=null&&len>0) {
			for(int i=0;i<len;i++) {
				list.add(objs[i]);
			}
		}
		return list;
	}
	
	public Lists add(Object obj) {
		list.add(obj);
		return this;
	}
	
	public List build() {
		return list;
	}
	
	public static List makeListByProp(List list, String prop) {
		Iterator iter = list.iterator();
		List newlist = new ArrayList();
		while(iter.hasNext()) {
			Object obj = iter.next();
			try {
				newlist.add(obj.getClass().getMethod("get"+StringUtils.capitalize(prop)).invoke(obj));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return newlist;
	}
	
	public static void main(String[] args) {
		TUser user1 = new TUser();
		user1.setName("aaa");
		user1.setAge(10);
		
		TUser user2 = new TUser();
		user2.setName("bbb");
		user2.setAge(12);
		
		System.out.println(makeListByProp(new Lists().add(user1).add(user2).build(), "name"));
	}

}
